package com.zhan.easyexcelvalidation.controller;

import com.zhan.easyexcelvalidation.model.ExcelData;
import com.zhan.easyexcelvalidation.util.ExcelReader;
import com.zhan.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author zhan
 * @since 2021-05-12 14:59
 */
@RestController
public class ImportController {
    @Autowired
    private  ExcelReader excelReader;
    @Autowired
    private  DataService dataService;

    @PostMapping("/excel/import")
    public ApiResponse importManufacturerInfo(@RequestPart MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        ExcelReader.Meta<ExcelData> excelDataMeta = new ExcelReader.Meta<>();
        excelDataMeta.setExcelStream(inputStream);
        excelDataMeta.setDomain(ExcelData.class);
        excelDataMeta.setHeadRowNumber(2);
        // 批量写入数据库的逻辑
        excelDataMeta.setConsumer(dataService::saveBatch);
        this.excelReader.read(excelDataMeta);
        return ApiResponse.success();
    }
}
